package com.zcoj.controller.User.user;

import com.zcoj.core.model.ApiResponse;
import com.zcoj.mail.service.EmailService;
import com.zcoj.pojo.Dto.User;
import com.zcoj.pojo.Vo.LoginPageVo;
import com.zcoj.pojo.Vo.RegisterPageVo;
import com.zcoj.security.dto.LoginUser;
import com.zcoj.service.ImageService;
import com.zcoj.service.UserService;
import com.zcoj.util.RedisCaches;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.Objects;

/**
 * 接口
 * @author 剩xia的盛夏
 * @date 2023/02/24 16:16
 */
@RestController
@RequestMapping("/user")
@Api(tags = "接口")
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private RedisCaches redisCaches;

    @Autowired
    private ImageService imageService;

    @Autowired
    private EmailService emailService;

    @Value("${spring.mail.username}")
    private String fromEmail;


    @GetMapping("/getUserInfoByID/{uid}")
    public ApiResponse getUserInfoByID(@PathVariable Integer uid){
        return ApiResponse.success(userService.getUserInfoByID(uid));
    }

    @GetMapping("/checkUserWxBind/{uid}")
    public ApiResponse checkUserWxBind(@PathVariable Integer uid){
        return userService.checkUserWxBind(uid);
    }

    //设置微信 关联账号
    @PostMapping("/setWxUserInfo")
    public ApiResponse setWxUserInfo(@RequestBody Map<String, String> map){
        String ticket=map.get("ticket");
        String uid = map.get("uid");

        return userService.setWxUserInfo(ticket,Integer.parseInt(uid));
    }

    @GetMapping("/userinfo")
    public ApiResponse getUserInfo(){
        LoginUser loginUser = redisCaches.getLoginUser();
        if(Objects.nonNull(loginUser)){
            User userServiceById = userService.findById(loginUser.getId());
            userServiceById.setPassword(null);
            return ApiResponse.success(userServiceById);
        }else {
            return ApiResponse.failed("非法请求");
        }
    }


    @PostMapping("/updateUserInfo")
    public ApiResponse updateUserInfo(@RequestBody User user){
        LoginUser loginUser = redisCaches.getLoginUser();
        if(Objects.nonNull(loginUser)){
            if (loginUser.getId().equals(user.getId())){
                Integer integer = userService.saveUserInfo(user);
                if(integer>0){
                    return ApiResponse.success("更新成功",null);
                }else {
                    return ApiResponse.failed("更新失败");
                }
            }
        }
            return ApiResponse.failed("非法请求");


    }


    /**
     * @method: [revisePassword]
     * @param: [passwordInfo]
     * @returns: com.zcoj.core.model.ApiResponse
     * @description:  用户登录后的修改密码接口
     * @author: chen.bin
     * @version: 1.0.0
     * @date 2023/5/11 17:43
     **/
    @PostMapping("/revisePassword")
    public ApiResponse revisePassword(@RequestBody Map<String, String> passwordInfo){
        return userService.revisePassword(passwordInfo);
    }


    /**
     * @method: [reviseEmail]
     * @param: [emailInfo]
     * @returns: com.zcoj.core.model.ApiResponse
     * @description:  用户登录后修改邮箱接口
     * @author: chen.bin
     * @version: 1.0.0
     * @date 2023/5/11 17:45
     **/
    @PostMapping("/reviseEmail")
    public ApiResponse reviseEmail(@RequestBody Map<String, String> emailInfo){
        return userService.reviseEmail(emailInfo);
    }
    /**
     * 通过ID查询
     * @author 剩xia的盛夏
     * @date 2023/02/24 16:16
     */


    @ApiOperation("登录")
    @PostMapping("/login")
    public ApiResponse loginByUsername(@RequestBody LoginPageVo loginPageVo){
        log.info("用户登录信息{}",loginPageVo);
        return  userService.verificationCode(loginPageVo);
    }

    @PostMapping("/login/ticket")
    public ApiResponse loginByWxTicket(@RequestBody Map<String, String> map){
        String ticket = map.get("ticket");
        return userService.loginByWxTicket(ticket);

    }

    @PostMapping("/adminLogin")
    public ApiResponse adminLogin(@RequestBody Map<String, String> map){
        return userService.adminLogin(map);
    }

    @ApiOperation("用户登录验证码")
    @PostMapping("/login/emailCode")
    public ApiResponse sendEmailCodePost(@RequestBody LoginPageVo loginPageVo){
        return userService.sendEmailCodePost(loginPageVo);
    }

    /**
     * @method: [sendEmailCode]
     * @param: [email]
     * @returns: com.zcoj.core.model.ApiResponse
     * @description:  放行接口，未登录时访问
     * @author: chen.bin
     * @version: 1.0.0
     * @date 2023/5/11 20:25
     **/
    @ApiOperation("发送验证码")
    @GetMapping ("/sendEmailCode/{email}")
    public ApiResponse sendEmailCode(@PathVariable String email){
        // ApiResponse.failed("验证码未失效");ApiResponse.success("发送成功",null);
        Integer sendEmailCode = userService.sendEmailCode(email);
        if(sendEmailCode==0){
            return ApiResponse.failed("验证码未失效");
        }else {
            return ApiResponse.success("发送成功",null);
        }
    }

    @ApiOperation("发送验证码")
    @GetMapping ("/sendEmailCodeHasRole/{email}")
    public ApiResponse sendEmailCodeHasRole(@PathVariable String email){
        // ApiResponse.failed("验证码未失效");ApiResponse.success("发送成功",null);
        Integer sendEmailCode = userService.sendEmailCode(email);
        if(sendEmailCode==0){
            return ApiResponse.failed("验证码未失效");
        }else {
            return ApiResponse.success("发送成功",null);
        }
    }

    /**
     * @method: [retrievePassword]
     * @param: [loginPageVo]
     * @returns: com.zcoj.core.model.ApiResponse
     * @description:  用户未登录状态下设置
     * @author: chen.bin
     * @version: 1.0.0
     * @date 2023/5/11 18:25
     **/
    @ApiOperation("重置密码")
    @PostMapping("/retrievePassword")
    public ApiResponse retrievePassword(@RequestBody LoginPageVo loginPageVo){
        log.info("用户修改的信息{}",loginPageVo);
        return userService.retrievePassword(loginPageVo);
    }

    @ApiOperation("注册用户")
    @PostMapping("/registerUser")
    public ApiResponse registerUser(@RequestBody RegisterPageVo registerPageVo){
        log.info("用户注册的信息{}",registerPageVo);
        return userService.registerUser(registerPageVo);
    }

    @ApiOperation("退出登录")
    @GetMapping("/logout")
    public ApiResponse logoutUser(){
        return userService.logout();
    }

    @GetMapping("/getImageBase64")
    public ApiResponse getImageBase64(){
        String indexImageBase64 = imageService.getIndexImageBase64();
        return ApiResponse.success("",indexImageBase64);
    }

}
